package com.aidex.admin.controller.system;

import com.aidex.common.constant.Constants;
import com.aidex.common.core.domain.R;
import com.aidex.common.core.domain.entity.SysMenu;
import com.aidex.common.core.domain.entity.SysUser;
import com.aidex.common.core.domain.model.LoginBody;
import com.aidex.common.core.redis.RedisCache;
import com.aidex.common.utils.SecurityUtils;
import com.aidex.common.utils.StringUtils;
import com.aidex.framework.web.service.SysLoginService;
import com.aidex.framework.web.service.SysPermissionService;
import com.aidex.system.domain.SysNotice;
import com.aidex.system.service.SysMenuService;
import com.aidex.system.service.SysNoticeService;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 登录验证
 */
@RestController
public class SysLoginController {

    @Autowired
    private SysLoginService loginService;

    @Autowired
    private SysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private SysNoticeService noticeService;

    @Autowired
    private RedisCache redisCache;

    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    public R login(@RequestBody LoginBody loginBody) {
        JSONObject ajax = new JSONObject();
        switch (loginBody.getType()) {
            case 1:
                if (StringUtils.isEmpty(loginBody.getUsername())) {
                    return R.fail("用户名不能为空");
                }
                if (StringUtils.isEmpty(loginBody.getPassword())) {
                    return R.fail("密码不能为空");
                }
                break;
            case 2:
                if (StringUtils.isEmpty(loginBody.getMobile())) {
                    return R.fail("手机号不能为空");
                }
                if (StringUtils.isEmpty(loginBody.getMobileCode())) {
                    return R.fail("手机验证码不能为空");
                }
                break;
            case 3:
                if (StringUtils.isEmpty(loginBody.getEmail())) {
                    return R.fail("电子邮箱不能为空");
                }
                if (StringUtils.isEmpty(loginBody.getEmailCode())) {
                    return R.fail("电子邮箱验证码不能为空");
                }
                break;
        }
        // 生成令牌
        String token = loginService.login(loginBody);
        ajax.put(Constants.TOKEN, token);
        return R.data(ajax);
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping("getInfo")
    public R getInfo() {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        JSONObject ajax = new JSONObject();
        //获取用户自定义首页
//        Map<String, Object> resultMap = sysPortalConfigService.findUserConfigList(user);
//        if (resultMap != null) {
//            List<SysPortalConfig> userPortalConfig = (List<SysPortalConfig>) resultMap.get("portalList");
//            SysPortalConfig defaultSysPortalConfig = (SysPortalConfig) resultMap.get("default");
//            ajax.put("userPortalConfig" , userPortalConfig);
//            ajax.put("defaultPortalConfig" , defaultSysPortalConfig);
//        }
        String lincenseInfo = redisCache.getStringValue("aidex.license.info");
        ajax.put("user" , user);
        ajax.put("roles" , roles);
        ajax.put("permissions" , permissions);
        ajax.put("lincenseInfo" , lincenseInfo);
        return R.data(ajax);
    }

    /**
     * 获取路由信息
     *
     * @return
     */
    @GetMapping("getRouters")
    public R getRouters() {
        // 用户信息
        SysUser user = SecurityUtils.getLoginUser().getUser();
        List<SysMenu> menus = menuService.selectList(new QueryWrapper<>(), user.getId());
        return R.data(menuService.buildMenus(menus));
    }
}
